package databinding.demo.ui;

import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;

import databinding.demo.R;
import databinding.demo.databinding.ActivityBindObservableBinding;
import databinding.demo.model.ArticleInfo;
import databinding.demo.model.ObservableUser;
import databinding.demo.presenter.EditPresenter;
import databinding.demo.utils.LogUtils;

/**
 * Created by dengdayi
 * Date: 2017/03/23 10：46
 * 入门篇-observable对象
 */

public class BindObservableActivity extends AppCompatActivity {
    private ActivityBindObservableBinding binding;
    private ObservableUser user;
    private ArticleInfo articleInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_bind_observable);
        user = new ObservableUser("王", "晓儿");
        binding.setUser(user);

        articleInfo = new ArticleInfo();
        articleInfo.setTitle(new ObservableField<>("测试"));
        articleInfo.setDesc(new ObservableField<>("描述"));
        articleInfo.setReadCount(new ObservableInt(10));
        binding.setArticleInfo(articleInfo);
        setListener();
    }

    private void setListener() {
        binding.setOnOtherNameClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.getInstance().logE("user.getFirstName() : " + user.getFirstName() + "\nuser.getLastName() : " + user.getLastName());
            }
        });
        binding.setOnOtherNameSecondClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.getInstance().logE(
                        "标题： " + binding.getArticleInfo().getTitle().get()
                                + "\n描述 : " + binding.getArticleInfo().getDesc().get()
                                + "\n阅读数量： " + binding.getArticleInfo().getReadCount().get());
            }
        });
        binding.setEditPresenter(new EditPresenter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                user.setFirstName(s.toString());
            }
        });
    }
}
